﻿app.controller('GroupRoleController', function ($scope, $rootScope, groupRoleFactory, roleFactory, groupFactory, $compile, notificationFactory) {

    init();

    //all implement data model call in a init() function

    function init() {
        $scope.roleByGroup = JSON.parse(localStorage.getItem("menu"));
        $scope.userName = $rootScope.userName;
        //count--------------
        notificationFactory.countAddFriend(localStorage.getItem("idkey")).success(function (data) {
            $scope.countAddFriend = data;
        });
        notificationFactory.countFromRoom(localStorage.getItem("idkey")).success(function (data) {
            $scope.countFromRoom = data;
        });
        notificationFactory.countNewMessage(localStorage.getItem("idkey")).success(function (data) {
            $scope.countNewMessage = data;
        });

        // tab
        $scope.groupRoleShow = true;
        $scope.groupRoleShowClick = function () {
            roleFactory.getRoles().success(function (datar) {
                $scope.roles = datar;
                roleCount = datar.length;
                groupFactory.getGroups().success(function (datag) {
                    $scope.groups = datag;
                    groupCount = datag.length;
                    groupRoleFactory.gets().success(function (data) {
                        for (var x = 0; x < $scope.roles.length; x++) {
                            var temp = [];
                            for (var y = 0; y < $scope.groups.length; y++) {
                                var item = { Id: null, Value: null, GroupId: null, RoleId: null };
                                item.GroupId = $scope.groups[y].Id;
                                item.RoleId = $scope.roles[x].Id;
                                for (var z = 0; z < data.length; z++) {
                                    if (data[z].GroupId === $scope.groups[y].Id && data[z].RoleId === $scope.roles[x].Id) {
                                        item.Id = data[z].Id;
                                        item.Value = true;
                                        break;
                                    }
                                    else
                                        item.Value = false;
                                }
                                temp[y] = item;
                            }
                            arr[x] = temp;
                        }
                        $scope.groupRoles = arr;
                    });
                });
            });
            $scope.groupShow = false;
            $scope.groupRoleShow = true;
            $scope.roleShow = false;
        };
        $scope.roleShow = false;
        $scope.groupShowClick = function () {
            groupFactory.getGroups().success(function (data) {
                $scope.groups = data;
            });
            $scope.groupShow = true;
            $scope.roleShow = false;
            $scope.groupRoleShow = false;
        };
        $scope.groupShow = false;
        $scope.roleShowClick = function () {
            $scope.groupShow = false;
            $scope.roleShow = true;
            $scope.groupRoleShow = false;
        };

        //load data--------------------------------------------------
        var roleCount = 0;
        var groupCount = 0;
        var arr = [];
        $scope.roles = {};
        $scope.groupRoles = {};
        roleFactory.getRoles().success(function (datar) {
            $scope.roles = datar;
            roleCount = datar.length;
            groupFactory.getGroups().success(function (datag) {
                $scope.groups = datag;
                groupCount = datag.length;
                groupRoleFactory.gets().success(function (data) {
                    for (var x = 0; x < $scope.roles.length; x++) {
                        var temp = [];
                        for (var y = 0; y < $scope.groups.length; y++) {
                            var item = { Id: null, Value: null, GroupId: null, RoleId: null };
                            item.GroupId = $scope.groups[y].Id;
                            item.RoleId = $scope.roles[x].Id;
                            for (var z = 0; z < data.length; z++) {
                                if (data[z].GroupId === $scope.groups[y].Id && data[z].RoleId === $scope.roles[x].Id) {
                                    item.Id = data[z].Id;
                                    item.Value = true;
                                    break;
                                }
                                else
                                    item.Value = false;
                            }
                            temp[y] = item;
                        }
                        arr[x] = temp;
                    }
                    $scope.groupRoles = arr;
                });
            });
        });


        //role

        $scope.roleItem = { RoleId: null, RoleName: null, Description: null };
        $scope.addRolePanel = false;
        $scope.addRoleHeader = function () {
            $scope.addRolePanel = !$scope.addRolePanel;
        };
        $scope.addRole = function () {
            roleFactory.postRole($scope.roleItem).success(function () {
                $scope.roles = roleFactory.getRoles();
            });
        };
        $scope.delRole = function (id) {
            $('#role_' + id).fadeOut();
            roleFactory.deleteRole(id);
        };

        //group



        $scope.groupItem = { GroupName: null, Description: null, SystemValue: null };
        $scope.addGroupPanel = false;
        $scope.addGroupHeader = function () {
            $scope.addGroupPanel = !$scope.addGroupPanel;
        };
        $scope.addGroup = function () {
            groupFactory.postGroup($scope.groupItem).success(function () {
                groupFactory.getGroups().success(function (data) {
                    $scope.groups = data;
                });
            });
        };
        $scope.delGroup = function (group) {
            var check = false;
            for (var g = 0; g < $scope.groups.length; g++) {
                if ($scope.groups[g].Id == group.Id)
                    break;
            }
            if ($scope.groupRoles[0][g] == undefined) {
                check = false;
            } else {
                for (var i = 0; i < $scope.groupRoles.length; i++) {
                    if ($scope.groupRoles[i][g].GroupId == group.Id)
                        if ($scope.groupRoles[i][g].Value == true) {
                            check = true; break;
                        }
                }
            }
            if (check) {
                alert("Nhóm này hiện đang được sử dụng!");
            } else {
                groupFactory.deleteGroup(group.Id);
                $("#group_" + group.Id).remove();
                //groupFactory.getGroups().success(function (data) {
                //    $scope.groups = data;
                //});
            }

        };

        //rolegroup
        $scope.changeGroupRole = function (groupId, roleId, id) {
            var dataPost = { Id: id, GroupId: groupId, RoleId: roleId };
            var temp = $scope.groupRoles;
            if (id == null)
                groupRoleFactory.postGroupRole(dataPost).success(function (data) {
                    if (data > 0) {
                        for (var z = 0; z < temp.length; z++) {
                            for (var t = 0; t < $scope.groups.length; t++) {
                                if (temp[z][t].GroupId === groupId && temp[z][t].RoleId === roleId) {
                                    temp[z][t].Value = true;
                                    temp[z][t].Id = data;
                                    break;
                                }
                            }
                        }
                        $scope.groupRoles = [];
                        $scope.$watchCollection($scope.groupRoles, function () {
                            $scope.groupRoles = temp;
                        });
                    }

                });
            else
                groupRoleFactory.delGroupRole(id).success(function (data) {
                    if (data > 0) {
                        for (var z = 0; z < temp.length; z++) {
                            for (var t = 0; t < $scope.groups.length; t++) {
                                if (temp[z][t].Id === id) {
                                    temp[z][t].Value = false;
                                    temp[z][t].Id = null;

                                    break;
                                }
                            }
                        }
                        $scope.groupRoles = [];
                        $scope.$watchCollection($scope.groupRoles, function () {
                            $scope.groupRoles = temp;
                        });
                    }

                });
        };
        $scope.showItemGroupRole = true;
        $scope.test = function () {
            $scope.groupRoles = [];
        };

    }
    createMenu();
});